package com.industrial.operation.controller;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.operation.domain.OperationJd;
import com.industrial.operation.service.IOperationJdFxService;
import com.industrial.system.api.domain.SysUser;
import com.industrial.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 季度分报告
 */
@RestController
@RequestMapping("/operation/jdfx")
public class OperationJdFxController extends BaseController {

    @Autowired
    private IOperationJdFxService operationJdFxService;

    @Value("${jd.templatePath}")
    private String templateFilePath;

    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response){

    }

    @GetMapping("/list")
    public TableDataInfo list(OperationJd operationJd){

        startPage();

//        public static final String PROVINCE = "p_01_011";
//        public static final String CITY = "s_01_011";
//        public static final String COUNTY = "x_01_011";
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(sysUser.isAdmin()){
            operationJd.setProvince("370000");
        }else{
            if(ArrayUtil.contains(roles.toArray(),"x_01_011")){
                operationJd.setCounty(sysUser.getDistrict());
                operationJd.setCity(sysUser.getCity());
                operationJd.setProvince(sysUser.getProvince());
            }else if(ArrayUtil.contains(roles.toArray(),"s_01_011")){
                operationJd.setCity(sysUser.getCity());
                operationJd.setProvince(sysUser.getProvince());
            }else if(ArrayUtil.contains(roles.toArray(),"p_01_011")){
                operationJd.setProvince(sysUser.getProvince());
            }
        }
        List<OperationJd> result = operationJdFxService.list(operationJd);
        return getDataTable(result);
    }

    /**
     * <p>
     *     导入数据文件
     * </p>
     * @param multipartFile
     * @param operationJd
     * @return
     */
    @PostMapping("/importData")
    public AjaxResult importData(@RequestPart("file")MultipartFile multipartFile,OperationJd operationJd){
        if(StringUtils.isBlank(operationJd.getQuarter()) || StringUtils.isBlank(operationJd.getYears())){
            return AjaxResult.warn("请选择导入的季度");
        }
        operationJdFxService.importData(multipartFile,operationJd);
        return AjaxResult.success();
    }

    /**
     * 下载文件模版
     * @param response
     */
    @RequiresPermissions("operation:jd:dowloadTemplateFile")
    @PostMapping("/dowloadTemplateFile")
    public void dowloadTemplateFile(HttpServletResponse response){
        try{
            File file = new File(templateFilePath);
            // 获取文件名
            String filename = file.getName();
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
            // 将文件写入输入流
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStream fis = new BufferedInputStream(fileInputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();

            // 清空response
            response.reset();
            // 设置response的Header
            response.setCharacterEncoding("UTF-8");
            //Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
            //attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
            // filename表示文件的默认名称，因为网络传输只支持URL编码的相关支付，因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
            // 告知浏览器文件的大小
            response.addHeader("Content-Length", "" + file.length());
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(buffer);
            outputStream.flush();
        }catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 查询用地税收表格
     * @param operationJd
     * @return
     */
    @GetMapping("/selectJdYdssTable")
    public AjaxResult selectJdYdssTable(OperationJd operationJd){
        List<OperationJd> result = operationJdFxService.selectJdYdssTable(operationJd);

        return AjaxResult.success(result);
    }

    /**
     * 查询用地税收表格
     * @param operationJd
     * @return
     */
    @GetMapping("/selectJdYdssCharts")
    public AjaxResult selectJdYdssCharts(OperationJd operationJd){
        Map<String,Object> result = operationJdFxService.selectJdYdssCharts(operationJd);
        return AjaxResult.success(result);
    }


    /**
     * 查询用单位用地销售表格
     * @param operationJd
     * @return
     */
    @GetMapping("/selectJdYdysxsTable")
    public AjaxResult selectJdYdysxsTable(OperationJd operationJd){
        List<OperationJd> result = operationJdFxService.selectJdYdysxsTable(operationJd);

        return AjaxResult.success(result);
    }

    /**
     * 查询用单位用地销售图表
     * @param operationJd
     * @return
     */
    @GetMapping("/selectJdYdysxsCharts")
    public AjaxResult selectJdYdysxsCharts(OperationJd operationJd){
        Map<String,Object> result = operationJdFxService.selectJdYdysxsCharts(operationJd);
        return AjaxResult.success(result);
    }

    /**
     * 查询城市的税收
     * @param operationJd
     * @return
     */
    @GetMapping("/queryCitySsTable")
    public AjaxResult queryCitySsTable(OperationJd operationJd){
        List<OperationJd> result = operationJdFxService.queryCitySsTable(operationJd);
        return AjaxResult.success(result);
    }

    /**
     * 查询城市的主要销售收入表格
     * @param operationJd
     * @return
     */
    @GetMapping("/queryCityMiTable")
    public AjaxResult queryCityMiTable(OperationJd operationJd){
        List<OperationJd> result = operationJdFxService.queryCityMiTable(operationJd);
        return AjaxResult.success(result);
    }

    /**
     * 查询城市的主要销售收入图表
     * @param operationJd
     * @return
     */
    @GetMapping("/queryCityMiCharts")
    public AjaxResult queryCityMiCharts(OperationJd operationJd){
        Map<String, Object> result = operationJdFxService.queryCityMiCharts(operationJd);

        return AjaxResult.success(result);
    }

    /**
     * 查询城市的税收图表
     * @param operationJd
     * @return
     */
    @GetMapping("/queryCitySsCharts")
    public AjaxResult queryCitySsCharts(OperationJd operationJd){
        Map<String, Object> result = operationJdFxService.queryCitySsCharts(operationJd);

        return AjaxResult.success(result);
    }
}
